let pageNum = 1;
let totalPage;
$(function () {
    $.ajax({
        url:"/inboundStatics/JoinCountDetail",
        type:"post",
        datatype:"json",
        success:function (r) {
            console.log(r)
            if (r!=null){
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    t += "<tr>\n" +
                        "          <td>"+(i++)+"</td>\n" +
                        "          <td class='warename' id='"+r.data[j].warehousename+"'>"+r.data[j].warehousename+"</td>\n" +
                        "          <td class='purchaseid' id='"+r.data[j].purchaseid+"'>"+r.data[j].purchaseid+"</td>\n" +
                        "          <td>"+r.data[j].goodscomment+"</td>\n" +
                        "          <td>"+r.data[j].totalprice+"</td>\n" +
                        "          <td>"+r.data[j].joincounttime+"</td>\n" +
                        "          <td>"+r.data[j].joincountman+"</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toCPurchase(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          </td>\n" +
                        "        </tr>";
                }
                $("tbody").html(t);
                $(".paginList").html("<li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\" onclick=\"prevPage(this)\"></span></a></li><li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagenxt\" onclick=\"nextPage(this)\"></span></a></li>")
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
                totalPage = r.totalPage;
                nextPage();
                prevPage();
            }else {
                $("tbody").html("");
            }
        }
    })
})

function toCPurchase(a) {
    var purchaseid = $(a).parents().siblings(".purchaseid").attr("id");
    // console.log(purchaseid);
    $.ajax({
        url:"/inboundStatics/toCPurchase",
        type: "POST",
        data:{"purchaseid":purchaseid},
        success:function () {
            location.href = "/storage/stock/stockView.html";
        }
    })
}

function nextPage() {
    if (pageNum<totalPage){
        pageNum++;
    }
    $.ajax({
        url:"/inboundStatics/JoinCountDetail",
        data:{"pageNum":pageNum},
        type:"post",
        datatype:"json",
        success:function (r) {
            console.log(r)
            var t = "";
            var i = 1;
            for (let j = 0; j < r.data.length; j++) {
                t += "<tr>\n" +
                    "          <td>"+(i++)+"</td>\n" +
                    "          <td class='warename' id='"+r.data[j].warehousename+"'>"+r.data[j].warehousename+"</td>\n" +
                    "          <td class='purchaseid' id='"+r.data[j].purchaseid+"'>"+r.data[j].purchaseid+"</td>\n" +
                    "          <td>"+r.data[j].goodscomment+"</td>\n" +
                    "          <td>"+r.data[j].totalprice+"</td>\n" +
                    "          <td>"+r.data[j].joincounttime+"</td>\n" +
                    "          <td>"+r.data[j].joincountman+"</td>\n" +
                    "          <td>\n" +
                    "          \t<a href=\"javascript:void(0)\" onclick='toCPurchase(this)' class=\"tablelink\">查看详情</a>\n" +
                    "          </td>\n" +
                    "        </tr>";
            }
            $("tbody").html(t);
            $(".message>i").eq(0).html(r.totalRows);
            $(".message>i").eq(1).html(r.pageNum);
            totalPage = r.totalPage;
        }
    })
}
function prevPage() {
    if (pageNum>1){
        pageNum--;
    }
    $.ajax({
        url:"/inboundStatics/JoinCountDetail",
        data:{"pageNum":pageNum},
        type:"post",
        datatype:"json",
        success:function (r) {
            console.log(r)
            var t = "";
            var i = 1;
            for (let j = 0; j < r.data.length; j++) {
                t += "<tr>\n" +
                    "          <td>"+(i++)+"</td>\n" +
                    "          <td class='warename' id='"+r.data[j].warehousename+"'>"+r.data[j].warehousename+"</td>\n" +
                    "          <td class='purchaseid' id='"+r.data[j].purchaseid+"'>"+r.data[j].purchaseid+"</td>\n" +
                    "          <td>"+r.data[j].goodscomment+"</td>\n" +
                    "          <td>"+r.data[j].totalprice+"</td>\n" +
                    "          <td>"+r.data[j].joincounttime+"</td>\n" +
                    "          <td>"+r.data[j].joincountman+"</td>\n" +
                    "          <td>\n" +
                    "          \t<a href=\"javascript:void(0)\" onclick='toCPurchase(this)' class=\"tablelink\">查看详情</a>\n" +
                    "          </td>\n" +
                    "        </tr>";
            }
            $("tbody").html(t);
            $(".message>i").eq(0).html(r.totalRows);
            $(".message>i").eq(1).html(r.pageNum);
            totalPage = r.totalPage;
        }
    })
}
